import '../../../../helper/global/user/logic.dart';
import '../../../../helper/utils/util.dart';
import '../../../../helper/utils/validate.dart';
import 'state.dart';

class LoginPwdController extends GetxController {
  final state = LoginPwdState();
  @override
  void onInit() {
    super.onInit();
  }

  void actionForgot() {
    Get.toNamed(RouterPath.forgot_pwd);
  }

  void sure() async {
    if (state.old.isEmpty) {
      Hud.toast('请输入原密码');
      return;
    }
    if (state.newPwd.isEmpty) {
      Hud.toast('请输入新密码');
      return;
    }
    if (!Validate.matchPwd(input: state.newPwd)) {
      Hud.toast('密码长度8~16，包含数字、大小写字母、特殊字符中至少3种!');
      return;
    }

    if (state.newPwd1.isEmpty) {
      Hud.toast('请输入确认密码');
      return;
    }
    if (state.newPwd != state.newPwd1) {
      Hud.toast('两次密码输入不一致');
      return;
    }

    var res = await Api.updatePassword(
        old: state.old, newPwd: state.newPwd, repeatPwd: state.newPwd1);
    if (res.isSuccess) {
      Hud.toast('密码修改成功！请重新登录');
      UserController().loginOutSuccess();
      Get.offAllNamed(RouterPath.login);
    }
  }
}
